package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    DistrictInfoService districtInfoService;
    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    FeeValueService feeValueService;
    @Autowired
    FeeKeyService feeKeyService;
    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Override
    public IPage<ApartmentItemVo> pageApartmentItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItem(page,queryVo);
    }

    @Override
    @Transactional
    public void insertOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {

        Long iId = apartmentSubmitVo.getId();

        //将区县，省份，城市名称保存到 apartmentSubmitVo
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());

        //更新或新增主表 公寓信息表
        this.saveOrUpdate(apartmentSubmitVo);

        Long id = apartmentSubmitVo.getId();
        //判断是否为更新操作  若果是就删除中间表、再添加
        if (iId != null){

            //删除图片信息
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(graphQueryWrapper);

            //删除配套信息
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(facilityQueryWrapper);

            //删除标签信息
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(labelQueryWrapper);

            //删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueQueryWrapper = new LambdaQueryWrapper<>();
            feeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(feeValueQueryWrapper);
        }


        //添加

        //添加公寓配套关系
        apartmentSubmitVo.getFacilityInfoIds().forEach(facId->{
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(id).facilityId(facId).build();
            apartmentFacilityService.save(apartmentFacility);
        });

        //添加公寓标签关系
        apartmentSubmitVo.getLabelIds().forEach(labId->{
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(id).labelId(labId).build();
            apartmentLabelService.save(apartmentLabel);
        });

        //添加公寓杂费关系
        apartmentSubmitVo.getFeeValueIds().forEach(feeId->{
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeId).build();
            apartmentFeeValueService.save(apartmentFeeValue);
        });

        //添加图片信息
        apartmentSubmitVo.getGraphVoList().forEach(graphVo -> {
//            GraphInfo graphInfo = GraphInfo.builder().itemId(id).itemType(ItemType.APARTMENT).name(graphVo.getName()).url(graphVo.getUrl()).build();
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemId(id);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setUrl(graphVo.getUrl());
            graphInfoService.save(graphInfo);
        });

    }

    @Override
    @Transactional
    public ApartmentDetailVo selectDetailById(Long id) {

        //查询图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getIsDeleted, 0);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = GraphVo.builder()
                    .name(graphInfo.getName())
                    .url(graphInfo.getUrl())
                    .build();
            graphVoList.add(graphVo);
        });

        //查询公寓配套
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getIsDeleted, 0);
        List<ApartmentFacility> apartmentFacilities = apartmentFacilityService.list(apartmentFacilityLambdaQueryWrapper);
        List<FacilityInfo> facilityInfos = new ArrayList<>();
        apartmentFacilities.forEach(apartmentFacility -> {
            facilityInfos.add(facilityInfoService.getById(apartmentFacility.getFacilityId()));
        });

        //查询标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getIsDeleted, 0);
        List<ApartmentLabel> apartmentLabels = apartmentLabelService.list(apartmentLabelLambdaQueryWrapper);
        List<LabelInfo> labelInfos = new ArrayList<>();
        apartmentLabels.forEach(apartmentLabel -> labelInfos.add(labelInfoService.getById(apartmentLabel.getLabelId())));

        //查询杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getIsDeleted, 0);
        List<ApartmentFeeValue> apartmentFeeValues = apartmentFeeValueService.list(apartmentFeeValueLambdaQueryWrapper);
        List<FeeValueVo> feeValueVos = new ArrayList<>();
        apartmentFeeValues.forEach(apartmentFeeValue -> {
            FeeValue feeValue = feeValueService.getById(apartmentFeeValue.getFeeValueId());
            FeeKey feeKey = feeKeyService.getById(feeValue.getFeeKeyId());
            FeeValueVo feeValueVo = new FeeValueVo();

            feeValueVo.setId(feeValue.getId());
            feeValueVo.setFeeKeyName(feeKey.getName());
//            BeanUtils.copyProperties(feeValue, feeValueVo);
            feeValueVo.setName(feeValue.getName());
            feeValueVo.setUnit(feeValue.getUnit());
            feeValueVo.setFeeKeyId(feeValue.getFeeKeyId());
            feeValueVos.add(feeValueVo);
        });

        //查询公寓信息
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);

        return apartmentDetailVo;
    }


    /**
     * 根据id删除
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {

        //查询该公寓是否有房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);
        //有就不能删除
        if (count > 0){
            throw new LeaseException(ResultCodeEnum.ADMIN_ROOM_EXITS);
        }

        //删除公寓信息
        this.removeById(id);
        //删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //删除公寓配套关系
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //删除标签关系
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //删除杂费关系
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

    }

}




